import axios from "./http";
import qs from "querystring";

const request = {
  async get(url: any, data: any) {
    try {
      const response = await axios.get(url, {params: data});
      const {data: res} = response;
      return new Promise((resolve) => {
        const {code} = res;
        if (code === 0) {
          resolve(res);
        } else {
          resolve(res);
        }
      }).catch(err => {
        console.log(err);
      });
    } catch (err) {
      console.log(err);
    }
  },
  async post(url: any, data: any) {
    try {
      const response = await axios.post(url, qs.stringify(data));
      const {data: res} = response;
      return new Promise((resolve, reject) => {
        const {code} = res;
        if (code === 0) {
          console.log("resolve =======================");
          resolve(res);
        } else {
          console.log("reject =======================");
          reject(res);
        }
      }).catch(err => {
        console.log("Promise catch =======================");
        console.log(err);
      });
    } catch (err) {
      console.log("try catch =======================");
      console.log(err);
    }
  }
};
export default request;
